'use strict';
var router = require('express').Router();
const util = require('../util/novel') ;

router.get('/', function(req, res, next) {
    const q = req.query.q||''
    const page = req.query.page||1;
    if(!q){
        res.render('search',{results:[],q})
    }else {
        util.query(q,page).then(results =>{
            res.render('search',{results:results[0].map(e=>{
                e.link = e.link.replace('http://m.biduo.cc','')
                    return e;
                }),q})
        }).catch(() =>  res.render('search',{results:[],q}))
    }
})
router.get('/novel', function(req, res, next) {
    if(req.query.url){
        util.novel(req.query.url).then(obj=>{
            let infos =  obj.author.replace(/\n/g,',').replace(/\s/g,'').split(',')
            infos.pop();
            obj.infos = infos;
            res.render('novel',{obj});
        })
    }
})
router.get('/chapter', function(req, res, next) {
    if(req.query.url){
        util.chapter(req.query.url).then(obj=>{
            res.render('chapter',obj);
        })
    }
})















module.exports = router;